/****************************************************
	文件：TimerContext.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/2/7 21:5:35
	功能：计时器 上下文
*****************************************************/

using System;
using HTFW.Consts;
using HTFW.Logger;

namespace HTFW.Core
{
    public class TimerContext : IContext
    {
        //--------------------  字段  -----------------//

        #region 字段

        private HTTimer mTimer;

        public ContextType ContextType { get; set; }
        public ContextScope ContextScope { get; set; }
        public Action<IContext> OnInit { get; set; }
        public Action<IContext> OnDispose { get; set; }

        #endregion


        //--------------------  生命周期  -----------------//

        #region 生命周期

        public void Init(params object[] args)
        {
            mTimer = TimerFactory.Instance.GetTimer();

            OnInit?.Invoke(this);
        }

        public void Dispose()
        {
            mTimer.Dispose();

            OnDispose?.Invoke(this);

            OnInit = null;
            OnDispose = null;
        }

        public void OnTimerUpdate()
        {
            mTimer.Update();
        }

        #endregion


        //--------------------  计时任务  -----------------//

        #region 计时任务

        /// <summary>
        /// 添加计时任务
        /// 返回此任务的全局ID
        /// </summary>
        /// <param name="callBack">计时任务回调</param>
        /// <param name="delayTime">延迟时间</param>
        /// <param name="timeUnit">时间单位</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public int AddTimeTask(Action<int> callBack, double delayTime, TimeUnit timeUnit = TimeUnit.MilliSecond,
            int loopCount = 1)
        {
            return mTimer.AddTimeTask(callBack, delayTime, timeUnit, loopCount);
        }

        /// <summary>
        /// 移除计时任务
        /// </summary>
        /// <param name="tID">任务的全局ID</param>
        public void RemoveTimeTask(int tID)
        {
            mTimer.RemoveTimeTask(tID);
        }

        /// <summary>
        /// 移除所有计时任务
        /// </summary>
        public void RemoveAllTimeTask()
        {
            mTimer.RemoveAllTimeTask();
        }

        /// <summary>
        /// 替换计时任务
        /// 返回bool（是否替换成功）
        /// </summary>
        /// <param name="tID">待替换任务的全局ID</param>
        /// <param name="callBack">计时任务回调</param>
        /// <param name="delayTime">延迟时间</param>
        /// <param name="timeUnit">时间单位</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public bool ReplaceTimeTask(int tID, Action<int> callBack, double delayTime,
            TimeUnit timeUnit = TimeUnit.MilliSecond, int loopCount = 1)
        {
            return mTimer.ReplaceTimeTask(tID, callBack, delayTime, timeUnit, loopCount);
        }

        #endregion


        //--------------------  帧任务  -----------------//

        #region 帧任务

        /// <summary>
        /// 添加帧任务
        /// 返回此任务的全局ID
        /// </summary>
        /// <param name="callBack">帧任务回调</param>
        /// <param name="delayFrame">延迟帧</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public int AddFrameTask(Action<int> callBack, int delayFrame, int loopCount = 1)
        {
            return mTimer.AddFrameTask(callBack, delayFrame, loopCount);
        }

        /// <summary>
        /// 移除帧任务
        /// </summary>
        /// <param name="tID">任务的全局ID</param>
        public void RemoveFrameTask(int tID)
        {
            mTimer.RemoveFrameTask(tID);
        }

        /// <summary>
        /// 移除所有帧任务
        /// </summary>
        public void RemoveAllFrameTask()
        {
            mTimer.RemoveAllFrameTask();
        }

        /// <summary>
        /// 替换帧任务
        /// 返回bool（是否替换成功）
        /// </summary>
        /// <param name="tID">待替换任务的全局ID</param>
        /// <param name="callBack">帧任务回调</param>
        /// <param name="delayFrame">延迟帧</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public bool ReplaceFrameTask(int tID, Action<int> callBack, int delayFrame, int loopCount = 1)
        {
            return mTimer.ReplaceFrameTask(tID, callBack, delayFrame, loopCount);
        }

        #endregion
    }
}